<?php

/**
 * ECSHOP 管理中心旅行社管理
 * ============================================================================
 */
 
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 活动列表页
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
    admin_priv('users_manage');


    /* 模板赋值 */
    $smarty->assign('full_page',   1);
    $smarty->assign('ur_here',     '旅行社列表');
    $smarty->assign('action_link', array('href' => 'mshop.php?act=add', 'text' => '添加旅行社'));

    $list = mshop_list();
// print_r($list);exit;

    $smarty->assign('mshop_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    if($_REQUEST['chose_pid'])
        $smarty->assign('chose_pid', 1);


    /* 显示商品列表页面 */
    assign_query_info();
    $smarty->display('mshop_list.htm');
}
/*------------------------------------------------------ */
//-- 解除佣金锁定
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'unlock_profit')
{
    $mshop_id=$_GET['id']?$_GET['id']:"";
    $result=0;

    if($mshop_id)
    {
      $sql=" update  ".$GLOBALS['ecs']->table('mshop')." set sum_profit=sum_profit+lock_profit,lock_profit=0 where mshop_id='$mshop_id'";
      $result=$db->query($sql);
    }
    if($result)
    {
       $sys_mes="解除佣金锁定，加入可提现佣金成功。";
    }
    else
    {
         $sys_mes="解除佣金锁定，加入可提现佣金失败。";
    }
    /* 清除缓存 */
    clear_cache_files();

    $links[] = array('text' => '返回旅行社列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
    sys_msg($sys_mes,0, $links);
    exit;
    
} 
/*------------------------------------------------------ */
//-- 解除会员绑定
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'unbinding')
{
   $id=intval($_REQUEST['mshop_id'])?$_REQUEST['mshop_id']:0;            

   $sys_mes="绑定已解除";
   $links[0] = array('text' => '返回旅行社列表', 'href' => "mshop.php?act=list" . list_link_postfix());
   if(!$id)
   {
      sys_msg($sys_mes, 0, $links);
   }

   $sql="update ".$GLOBALS['ecs']->table('mshop')." set user_id=''  where mshop_id=$id";
   $status=$GLOBALS['db']->query($sql);
   if($status)
   {
    $sys_mes="绑定已解除";
    $links[0] = array('text' => '返回旅行社详情', 'href' => "mshop.php?act=edit&id=$id");
    sys_msg($sys_mes, 0, $links); 
   }
   else
   {
     $sys_mes="绑定解除失败";
     $links[0] = array('text' => '返回旅行社详情', 'href' => "mshop.php?act=edit&id=$id&" . list_link_postfix());
      sys_msg($sys_mes, 0, $links);
   }


}
/*------------------------------------------------------ */
//-- 分页、排序、查询
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $list = mshop_list();

    $smarty->assign('mshop_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);


    if($_GET['chose_pid'] )
    $smarty->assign('chose_pid',1);

    make_json_result($smarty->fetch('mshop_list.htm'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('users_manage');

    $id = intval($_GET['id']);
    $mshop = mshop_info($id);

    if (empty($mshop))
    {
        make_json_error($_LANG['favourable_not_exist']);
    }
    $name = $mshop['user_name'];

    if($mshop['sum_profit']>1)
    {
        make_json_error('无法删除，尚有佣金未提现!');
    }
    $sql=" delete from ".$GLOBALS['ecs']->table('mshop')." where mshop_id=".$id;
  
    $status=$GLOBALS['db']->query($sql);


    /* 记日志 */
    admin_log('删除'.$mshop['user_name'].'的旅行社'.$id, 'remove', 'mshop');

    /* 清除缓存 */
    clear_cache_files();

    $url = 'mshop.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    ecs_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 批量操作
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'batch')
{

    /* 取得要操作的记录编号 */
    if (empty($_POST['checkboxes']))
    {
        sys_msg($_LANG['no_record_selected']);
    }
    else
    {
        /* 检查权限 */
        admin_priv('users_manage');


        $ids = $_POST['checkboxes'];
        if (isset($_POST['drop']))
        {
           

            /* 删除记录 */


            /* 记日志 */
            admin_log('', 'batch_remove', 'mshop');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' => '返回旅行社列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
            sys_msg($_LANG['batch_drop_ok']);
        }
        elseif (isset($_POST['cash_money']))
        {


            /*提现记录 */
            // require_once('Wxpay_delivery.php'); 
            // $Wxpay_delivery=new Wxpay_delivery();
            // $sql="SELECT m.mshop_id,m.sum_profit,u.openid,u.user_id ".
            //     "FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
            //     " left join ". $GLOBALS['ecs']->table('users') ." as u on m.user_id=u.user_id "
            //     ." where m.sum_profit>=1 and m.mshop_id ".db_create_in($ids);
            //  $data_list=$GLOBALS['db']->getAll($sql);
            //  $error_num=0;
            //  $right_num=0;

            // foreach ($data_list as $key => $value) {
            //     $mshop_id=$value['mshop_id'];
            //     $user_id=$value['user_id'];
            //     $openid=$value['openid'];
            //     $cash_money=intval($value['sum_profit']);
            //     $status=$Wxpay_delivery->busSendProfit($mshop_id,$user_id,$openid,$cash_money);
            //     if($status)
            //    $right_num++;
            //    else
            //     $error_num++;
            // }

            // $sys_mes="成功提现". $right_num."人，提现失败".$error_num."人，查看佣金提现记录了解详情。";





            /* 记日志 */
             admin_log('成功全部提现'.$mshop['mshop_id'], 'add', 'mshop');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' => '返回旅行社列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
            sys_msg($sys_mes);
        }
    }
}


/*------------------------------------------------------ */
//-- 添加、编辑
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add' || $_REQUEST['act'] == 'edit')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add';
    $smarty->assign('form_action', $is_add ? 'insert' : 'update');


    /* 初始化、取得优惠活动信息 */
    if ($is_add)
    {
        // $sql = 'SELECT c.cat_id,c.cat_name ' .
        //         ' FROM ' . $GLOBALS['ecs']->table('category') . ' as c ' .
        //         'left join ' . $GLOBALS['ecs']->table('touch_category') . ' as t on t.cat_id = c.cat_id ' .
        //         "WHERE c.parent_id = 0 AND c.is_show = 1 ORDER BY c.sort_order ASC, c.cat_id ASC";
        // $cats =$GLOBALS['db']->getAll($sql);

        $mshop = array(
            'mshop_id'        => 0,
            'mshop_name'        =>'',
             'mobile_phone'        => '',
            'sort_order'=>50,
            'sum_profit'    => 0,
            'p_user_name'=>0
        );
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }
        $id = intval($_GET['id']);
        $mshop = mshop_info($id);
        if (empty($mshop))
        {
            sys_msg($_LANG['favourable_not_exist']);
        }

    }
    $smarty->assign('mshop', $mshop);


    // print_r($mshop);exit();


    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', $_LANG['add_favourable']);
    }
    else
    {
        $smarty->assign('ur_here', $_LANG['edit_favourable']);
    }
    $href = 'mshop.php?act=list';
    if (!$is_add)
    {
        $href .= '&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => $_LANG['favourable_list']));
    assign_query_info();
    $smarty->display('mshop_info.htm');
}

/*------------------------------------------------------ */
//-- 添加、编辑后提交
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert';

    /* 检查旅行社是否重复 */
    $mshop_user_id = intval($_POST['mshop_user_id']) >= 0 ? intval($_POST['mshop_user_id']) : 0;
    $id=intval($_POST['id'])?intval($_POST['id']):0;


   //旅行社未绑定会员或未修改
    // $sql=" select mshop_id from".$GLOBALS['ecs']->table('mshop')." where mshop_id!='$id' and user_id='$mshop_user_id'";
    // $u_mshop_id=$GLOBALS['db']->getOne($sql);
    
    // if ($u_mshop_id)
    // {
    //     sys_msg($_LANG['act_name_exists']);
    // }

    /* 是否设置商品主营类别 */
    // if (!isset($_POST['cats']))
    // {
    //     sys_msg($_LANG['pls_set_cats']);
    // }
    // else
    // {
    //     $cat_ids=implode(',', $_POST['cats']);
    // }

    /* 检查可提现佣金 */
    $mshop_name = $_POST['mshop_name']? $_POST['mshop_name'] : 'XXXX';
    $mobile_phone = intval($_POST['mobile_phone'])? $_POST['mobile_phone'] : '';
    $sort_order =  intval($_POST['sort_order'])? $_POST['sort_order'] : '';
    $sum_profit = $_POST['sum_profit']? $_POST['sum_profit'] : 0;

    

    /* 提交值 */
    $mshop = array(
        'mshop_id'        => intval($_POST['id']),
        'mshop_name'        =>$mshop_name,
        'mobile_phone'        => $mobile_phone,
        'sort_order'=>$sort_order,
        'sum_profit'      => $sum_profit,
        'user_id'      => $mshop_user_id

    );

    /* 保存数据 */
    if ($is_add)
    {
        $now_time=gmtime();
        $mshop['ctime']=$now_time;

        $db->autoExecute($ecs->table('mshop'), $mshop, 'INSERT');
        $mshop['mshop_id'] = $db->insert_id();
         
    }
    else
    {
        $db->autoExecute($ecs->table('mshop'), $mshop, 'UPDATE', "mshop_id = '$mshop[mshop_id]'");
         
    }

    /* 记日志 */
    if ($is_add)
    {
        admin_log('添加旅行社'.$mshop['mshop_id'], 'add', 'mshop');
    }
    else
    {
        admin_log('编辑旅行社'.$mshop['mshop_id'], 'edit', 'mshop');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'mshop.php?act=add', 'text' => $_LANG['continue_add_favourable']),
            array('href' => 'mshop.php?act=list', 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['add_favourable_ok'], 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'mshop.php?act=list&' . list_link_postfix(), 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['edit_favourable_ok'], 0, $links);
    }
}

/*------------------------------------------------------ */
//-- 搜索商品
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'search')
{
    /* 检查权限 */
    check_authz_json('mshop');

    include_once(ROOT_PATH . 'includes/cls_json.php');

    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword);
    if ($filter->act_range == FAR_ALL)
    {
        $arr[0] = array(
            'id'   => 0,
            'name' => $_LANG['js_languages']['all_need_not_search']
        );
    }
    else
    {
       $arr=$mshop_list;
    }

    if (empty($arr))
    {
        $arr = array(0 => array(
            'id'   => 0,
            'name' => $_LANG['search_result_empty']
        ));
    }

    make_json_result($arr);
}
elseif ($_REQUEST['act'] == 'cash_all_money')
{
           /*提现记录 */
            require_once('Wxpay_delivery.php'); 
            $Wxpay_delivery=new Wxpay_delivery();
            $sql="SELECT m.mshop_id,m.sum_profit,u.openid,u.user_id ".
                "FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('users') ." as u on m.mshop_id=u.mshop_id "
                ." where m.sum_profit>=1  ";
             $data_list=$GLOBALS['db']->getAll($sql);
             $error_num=0;
             $right_num=0;

            foreach ($data_list as $key => $value) {
                $mshop_id=$value['mshop_id'];
                $user_id=$value['user_id'];
                $openid=$value['openid'];
                $cash_money=intval($value['sum_profit']);
                $status=$Wxpay_delivery->busSendProfit($mshop_id,$user_id,$openid,$cash_money);
                if($status)
               $right_num++;
               else
                $error_num++;
            }

            $sys_mes="成功提现". $right_num."人，提现失败".$error_num."人，查看佣金提现记录了解详情。";
  
   



            /* 记日志 */
             admin_log('成功提现'.$mshop['mshop_id'], 'add', 'mshop');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' => '返回旅行社列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
            sys_msg($sys_mes,0,$links);
}
elseif ($_REQUEST['act'] == 'mshop_build_url')//生成旅行社链接
{
           $mshop_id=intval($_GET['mshop_id'])?intval($_GET['mshop_id']):0;
           if(! $mshop_id){
                    $links[] = array('text' => '查看旅行社链接信息', 'href' => 'mshop.php?act=get_mshop_url&' . list_link_postfix());
                    sys_msg('参数为空，没有该旅行社',0,$links);
           }


            $sql="SELECT m.mshop_id,u.url_id,u.password,u.org_pwd ".
                "FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('mshop_build_url') ." as u on m.mshop_id=u.mshop_id  where m.mshop_id='$mshop_id'";
             $data_row=$GLOBALS['db']->getRow($sql);
             $url_id=$data_row['url_id']?$data_row['url_id']:0;


            /* 提交值 */
            $now_time=gmtime();
            $pwd=nb_random(20).$mshop_id;
            $mshop_build_url = array(
                'update_time'      =>$now_time,
                'org_pwd'        =>$pwd,
                'password'        =>md5($pwd),

            );

            /* 保存数据 */
            if (!$url_id)
            {
                
                $mshop_build_url['ctime']=$now_time;
                $mshop_build_url['mshop_id']=$mshop_id;

                $db->autoExecute($ecs->table('mshop_build_url'), $mshop_build_url, 'INSERT');
                $mshop_build_url['mshop_id'] = $db->insert_id();
                 
            }
            else
            {
                $db->autoExecute($ecs->table('mshop_build_url'), $mshop_build_url, 'UPDATE', "url_id = '$url_id'");
            }

         $sys_mes="生成链接成功";

           /* 记日志 */
         admin_log('生成链接，旅行社'.$mshop_id, 'mshop_build_url', 'mshop');

        /* 清除缓存 */
        clear_cache_files();

        $links[] = array('text' => '查看旅行社链接信息', 'href' => "mshop.php?act=get_mshop_url&mshop_id=$mshop_id&" . list_link_postfix());
        sys_msg($sys_mes,0,$links,false);
}
elseif ($_REQUEST['act'] == 'get_mshop_url')//查看旅行社链接
{
            $mshop_id=intval($_GET['mshop_id'])?intval($_GET['mshop_id']):0;

            $sql="SELECT m.mshop_id,m.mshop_name,u.url_id,u.org_pwd ".
                "FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('mshop_build_url') ." as u on m.mshop_id=u.mshop_id  where m.mshop_id='$mshop_id'";
             $data_row=$GLOBALS['db']->getRow($sql);

             if($data_row)
             {
                $data_row['url']="http://".$_SERVER['HTTP_HOST']."/app_api/index.php?m=home&c=url&a=mshop_url_login&mshop_id=$mshop_id";
              
                if($data_row['url_id'])
                {  
                    $smarty->assign('ur_here', "旅行社佣金查看链接");
                    $smarty->assign('data_row',$data_row);
                }
                else//没有生成过链接
                {
                    $sys_mes="该旅行社没有查看链接";
                    $links[] = array('text' => '去生成链接', 'href' => 'mshop.php?act=mshop_build_url&mshop_id='.$mshop_id.'&' . list_link_postfix());
                    sys_msg($sys_mes,1,$links);
                }

             }
             else
             {
                $sys_mes="不存在该旅行社";
                $links[] = array('text' => '返回旅行社列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
                sys_msg($sys_mes,1,$links);
             }
  
   

            /* 显示商品列表页面 */
            assign_query_info();
            $smarty->display('mshop_url.htm');



}
/*
 * 取得旅行社列表
 * @return   array
 */
function mshop_list()
{    
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['mshop_id']    = empty($_REQUEST['mshop_id']) ? '' : trim($_REQUEST['mshop_id']); 
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);  
        $filter['binding_mb']    = empty($_REQUEST['binding_mb']) ? '' : trim($_REQUEST['binding_mb']);  

        $filter['chose_pid']    = empty($_REQUEST['chose_pid']) ? 0: trim($_REQUEST['chose_pid']);  


       //销售额，佣金，冻结佣金
        $filter['min_amount']    = empty($_REQUEST['min_amount']) ? '' : trim($_REQUEST['min_amount']); 
        $filter['max_amount']    = empty($_REQUEST['max_amount']) ? '' : trim($_REQUEST['max_amount']); 
        $filter['max_profit']    = empty($_REQUEST['max_profit']) ? '' : trim($_REQUEST['max_profit']);
        $filter['min_profit']    = empty($_REQUEST['min_profit']) ? '' : trim($_REQUEST['min_profit']); 
        $filter['max_cash']    = empty($_REQUEST['max_cash']) ? '' : trim($_REQUEST['max_cash']);
        $filter['min_cash']    = empty($_REQUEST['min_cash']) ? '' : trim($_REQUEST['min_cash']); 
        

        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
            $filter['mshop_id'] = json_str_iconv($filter['mshop_id']);
            $filter['keyword'] = json_str_iconv($filter['keyword']);
            $filter['binding_mb'] = json_str_iconv($filter['binding_mb']);

            $filter['min_amount'] = json_str_iconv($filter['min_amount']);
            $filter['max_amount'] = json_str_iconv($filter['max_amount']);
            $filter['max_profit'] = json_str_iconv($filter['max_profit']);
            $filter['min_profit'] = json_str_iconv($filter['min_profit']);
            $filter['max_cash'] = json_str_iconv($filter['max_cash']);
            $filter['min_cash'] = json_str_iconv($filter['min_cash']);

            $filter['chose_pid'] = json_str_iconv($filter['chose_pid']);

        }

        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'm.mshop_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $where = "";
        if (!empty($filter['mshop_id']))
        {
            $where .= " AND m.mshop_id=" . mysql_like_quote($filter['mshop_id']);
        }
        if (!empty($filter['keyword']))
        {
            $where .= " AND m.mshop_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
        }
        if (!empty($filter['binding_mb']))
        {
            $where .= " AND u.user_name LIKE '%" . mysql_like_quote($filter['binding_mb']) . "%'";
        }



        if (!empty($filter['min_profit']))
        {
            $where .= " AND m.sum_profit>=" . mysql_like_quote($filter['min_profit']);
        }
        if (!empty($filter['max_profit']))
        {
           $where .= " AND m.sum_profit<=" . mysql_like_quote($filter['max_profit']);
        }        
        if (!empty($filter['min_cash']))
        {
            $where .= " AND m.sum_cash_amount>=" . mysql_like_quote($filter['min_cash']);
        }
        if (!empty($filter['max_cash']))
        {
           $where .= " AND m.sum_cash_amount<=" . mysql_like_quote($filter['max_cash']);
        }

        //选择旅行社-zhsf
        if($filter['chose_pid'])
        {$_GET['chose_pid']=1;}
       


      $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('mshop') ." as m  ".
        " left join ". $GLOBALS['ecs']->table('users') ." as u on u.user_id=m.user_id ".
        " WHERE 1 $where".$ts_where;
       $filter['record_count'] = $GLOBALS['db']->getOne($sql);



     /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT m.*".
                ",u.user_name,u.pay_points  ".
                " FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('users') ." as u on u.user_id=m.user_id ".
                " WHERE 1 $where ".$ts_where.
                " ORDER BY $filter[sort_by] $filter[sort_order] ".
                " LIMIT ". $filter['start'] .", $filter[page_size]";
      
        $filter['keyword'] = stripslashes($filter['keyword']);


        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);





    $list = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $row['ctime']=date('Y-m-d H:i:s',$row['ctime']);

        $list[] = $row;
    }

    return array('item' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

   function mshop_info($id)
   {
        $sql="SELECT m.*,u.user_name,u.pay_points ".
                "FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('users') ." as u on m.user_id=u.user_id "
                ." where m.mshop_id=".$id;
        $row=$GLOBALS['db']->getRow($sql);

      //    $sql = 'SELECT c.cat_id,c.cat_name ' .
      //           ' FROM ' . $GLOBALS['ecs']->table('category') . ' as c ' .
      //           'left join ' . $GLOBALS['ecs']->table('touch_category') . ' as t on t.cat_id = c.cat_id ' .
      //           "WHERE c.parent_id = 0 AND c.is_show = 1 ORDER BY c.sort_order ASC, c.cat_id ASC";
      //   $cate =$GLOBALS['db']->getAll($sql);
      //    //主营商品类别
      // if($row['cat_ids'])
      //  {
      //       $cids=explode(',', $row['cat_ids']);
      //       foreach ($cate as $key => &$value) {
      //           if(!(in_array($value['cat_id'],$cids)))
      //           {
      //              $value['no_selected']=1;   
      //           }
      //       }

      //   }
      //   $row['cats']=$cate;

        // $row['cat_names']='江左梅郎';


         
        return $row;
   }

   //随机数
    function nb_random($length = 6, $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double) microtime() * 1000000);
        if ($numeric) {
            $hash = sprintf('%0' . $length . 'd', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for ($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    } 
   
   
?>